import {Actions} from "react-native-router-flux";
import {QueryTasks} from "../services/tasksList";
export default {
  namespace: 'tasksList',

  state: {
    list: [],
      loading:true,
      page:1,
      pageSize:9,
      last:0,
      tabNum:0,
      prjname:'',
      rowData:''
  },

  subscriptions: {
  },

  effects: {
      *queryTasks({payload}, {put, select, call}){
          yield put({type:'getLoading'})
          let current = '';
          let TabNum = '';
          const {userId} = yield select(state => state.login)
          const {projectId} = yield select(state => state.projectsList)
          const {page,pageSize,tabNum,prjname,rowData} = yield select(state => state.tasksList)
          if(payload === '' || payload === undefined){
              current = page;
              TabNum = tabNum;
          }else {
              if (payload.page ==='' || payload.page === undefined){
                  current = page;
              }else {
                  current = payload.page;
              }
              if (payload.tabNum ==='' || payload.tabNum === undefined){
                  TabNum = tabNum;
              }else {
                  TabNum = payload.tabNum;
              }
          }
          const {json, error} = yield call(QueryTasks, {pageNo:current,pageSize:pageSize,
                                                        projectId: projectId, userId: userId,name:prjname,receiveStatus:rowData})
          if (error) {
              // console.log('Query Tasks failed: ' + error)
              yield put({type: 'cleanLoading'})
          } else {
              if (json.code == 1) {
                  yield put({type: 'querySuccess', payload: json.data})
              } else {
                  yield put({type: 'getLoading'})
              }
          }
      },
      *search({payload},{put,select,call}){
          yield put({type:'queryTasks',payload:{page:1}})
      },

    //清楚所有表单数据
    *cleanData({payload}, {call, put, select}){
      yield put({type: 'sign/cleanSignImgs'});
      yield put({type: 'photo/cleanPhotoImgs'});
      yield put({type: 'signIn/cleanData'});
      yield put({type: 'signOut/cleanData'});
      yield put({type: 'rectifyBill/resetData'});

    },
  },

  reducers: {
    querySuccess(state, {payload}){
        let num = payload.pageNo;
        let last = payload.last;
        let list = payload.list.map(val => {
        let tmp = [
          val.id,
            val.name,
          val.createDate.length>10?val.createDate.substring(0,10):val.createDate,
          // val.taskNum,

          // val.signInStatus !== val.signOutStatus,
          val.isChangeForm != 1,
          {
              signIn:val.signInStatus,signOut:val.signOutStatus,
            preStatus: '1', status: val.receiveStatus, value: val.receiveStatus !== '0' ? '已接收' : '未接收',
          },
          {
              signIn:val.signInStatus,signOut:val.signOutStatus,
            preStatus: val.receiveStatus, status: val.signInStatus, value: val.signInStatus === '1' ? '已签到' : '未签到',
          },
          {
              signIn:val.signInStatus,signOut:val.signOutStatus,
            preStatus: val.signInStatus, status: val.signOutStatus, value: val.signOutStatus === '1' ? '已签退' : '未签退',
          },
        ]
        return tmp
        })
        return {...state, list: list,page:num,last:last,loading:false}
    },
      getLoading(state,{payload}){
          return {...state,loading:true}
      },
      cleanLoading(state,{payload}){
          return{...state,loading:false}
      },
      getClean(state,{payload}){
          return {...state,loading:true,page:1,rowData:'',prjname:''}
      },
      thisTextChange(state,{payload}){
          return {...state,prjname:payload}
      },
      selectRowData(state,{payload}){
          if(payload === '已接收'){
              payload = '1';
          }else if(payload === '未接收'){
              payload = '0';
          }
          return {...state,rowData:payload}
      },
  },

}